//
//  MeBillDetailViewController.m
//  RainbowFM
//
//  Created by jv on 14-12-14.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "MeBillDetailViewController.h"
#import "OrderModel.h"
#import "PayViewController.h"

@interface MeBillDetailViewController ()
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (weak, nonatomic) IBOutlet UILabel *prodNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UIImageView *prodImageView;
@property (weak, nonatomic) IBOutlet UILabel *buyerNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyerPhoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyerAddrLabel;
@property (weak, nonatomic) IBOutlet UILabel *merchantNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *merchantPhoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *orderIdLabel;
@property (weak, nonatomic) IBOutlet UILabel *orderDateLabel;
@property (weak, nonatomic) IBOutlet UILabel *logisNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *logisNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *logisDateLabel;
@property (weak, nonatomic) IBOutlet UILabel *prodPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *payPriceLabel;
@property (weak, nonatomic) IBOutlet UIButton *confirmButton;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;

@end

@implementation MeBillDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViewData];
    // Do any additional setup after loading the view.
}

-(void)initViewData{
    self.statusLabel.text        = [self orderStatus];
    self.prodNameLabel.text      = _orderListItem.prodName;
    self.priceLabel.text         = _RMB(_orderListItem.prodTotalPrice);
    self.buyerNameLabel.text     = _orderListItem.buyerName;
    self.buyerPhoneLabel.text    = _orderListItem.buyerPhone;
    self.buyerAddrLabel.text     = _orderListItem.buyerAddr;
    self.merchantNameLabel.text  = _orderListItem.merchantName;
    self.merchantPhoneLabel.text = _orderListItem.merchantPhone;
    self.orderIdLabel.text       = _orderListItem.orderId;
    self.orderDateLabel.text     = _orderListItem.orderDate;
    
    self.payPriceLabel.text = _RMB(_orderListItem.prodTotalPrice);
}


-(NSString *)orderStatus{
    self.confirmButton.hidden = NO;
    self.cancelButton.hidden = NO;
    switch ([_orderListItem.orderStatus intValue]) {
        case 1:
            return @"待付款";
            break;
        case 2:
            self.confirmButton.hidden = YES;
            self.cancelButton.hidden = YES;
            return @"已过期";
            break;
        case 3:
            return @"支付失败";
            break;
        case 4:
            self.cancelButton.hidden = YES;
            [self.confirmButton setTitle:@"退款" forState:UIControlStateNormal];
            return @"支付成功，待发货";
            break;
        case 5:
            return @"支付成功，已发货";
            break;
        case 6:
            return @"支付成功，发货失败";
            break;
        case 7:
            return @"支付成功，交易成功";
            break;
        case 8:
            return @"支付成功，已退款";
            break;
        default:
            return @"";
            break;
    }
}


-(IBAction)confirmAction:(UIButton *)button{

    if ([_orderListItem.orderStatus integerValue] == 1) { //付款
        [[OrderModel sharedModel] paymentWithOrderID:_orderListItem.orderId payType:@"aliPay" result:^(BOOL success, NSString *payURL, NSString *msg) {
            PayViewController *payView = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"PayViewController"];
            payView.payURL = payURL;
            [self.navigationController pushViewController:payView animated:YES];
        }];
    }
}

-(IBAction)cancelAction:(UIButton *)button{
    
    if ([_orderListItem.orderStatus integerValue] == 1) { //取消订单
        [SVProgressHUD show];
        [[OrderModel sharedModel] cancelOrderWithOrderID:_orderListItem.orderId result:^(BOOL success, NSString *msg) {
            if (success) {
                [self.navigationController popToRootViewControllerAnimated:YES];
                [SVProgressHUD showSuccessWithStatus:msg];
            }else{
                [SVProgressHUD showErrorWithStatus:msg];
            }
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
